﻿using Dapper;
using System.Data;
using System.Linq;
using Model = Tauren.Business.Entity.Model;

namespace Tauren.Business.DLL.LogIn
{
    public class LogInBusiness : ILogInBusiness
    {
        public LogInBusiness(IDbConnection dbConnection)
        {
            this.dbConnection = dbConnection;
        }

        private IDbConnection dbConnection { get; set; }
        /// <summary>
        /// 根据账号和密码获取用户信息
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public Model.AccoutInfo GetAccoutInfo(string Account)
        {
            var account = dbConnection.Query<Model.AccoutInfo>("SELECT * FROM AccoutInfo WHERE account=@Account", new Entity.Model.AccoutInfo() { Account = Account }).FirstOrDefault();
        
            return account;
        }

        /// <summary>
        /// 根据账号和密码获取用户信息
        /// </summary>System.NotSupportedException:“Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.”

        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public Model.Employee GetEmployeeInfo(int EmpId)
        {
            var emp = dbConnection.Query<Model.Employee>("SELECT * FROM Employee WHERE empId=@EmpId", new Entity.Model.Employee()
            {
                EmpId = EmpId
            }).FirstOrDefault();        
            return emp;
        }

        /// <summary>
        /// 判断当前用户角色
        /// </summary>
        /// <param name="EmpId"></param>
        /// <param name="dbConnection"></param>
        /// <returns></returns>
        public bool IsCurrentRoleByAdmin(int EmpId)
        {
            bool b = false;
            var roles = dbConnection.Query<Model.Roleinfo>($"select r.* from roleinfo r inner join rolepersoninfo p on p.roleid =r.autoid where p.empid={EmpId} and r.code='admin'");
            if (roles.Any())
            {
                b = true;
            }        
            return b;
        }
        /// <summary>
        /// 获取组织名称
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public Model.Organization GetOrganizationInfo(int OrgID)
        {
            var emp = dbConnection.Query<Model.Organization>("select * from organization   where  orgid =@OrgId", new Entity.Model.Organization()
            {
                OrgId = OrgID
            }).FirstOrDefault();
        
            return emp;
        }

        /// <summary>
        /// 获取组织名称
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public Model.Position GetPositionInfo(int PostionId)
        {
            var emp = dbConnection.Query<Model.Position>("SELECT * FROM  `position` where  positionid = @PositionId", new Entity.Model.Position()
            {
                PositionId = PostionId
            }).FirstOrDefault();
        
            return emp;
        }

    }
}
